package com.zhangyj.impl.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.zhangyj.dao.MenuAndTreeDao;
import com.zhangyj.db.DBUtil;
import com.zhangyj.entitis.Program;

public class MenuAndTreeDaoImpl extends IbatisDao implements MenuAndTreeDao {
	public MenuAndTreeDaoImpl(SqlSessionFactory factory) {
		super(factory);
	}

	public List<?> getTreeItemList(int templateId) throws SQLException {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession(true);
			List<?> menus = sqlSession.selectList("menutree.loadAll", templateId);

			return menus;
		} finally {
			if (sqlSession != null)
				sqlSession.close();
		}
	}

	public Set<Integer> getProgramIdSet(int userid) throws SQLException {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession(true);
			List<?> list = sqlSession.selectList("menutree.progIds", userid);
			Set<Integer> set = new HashSet<Integer>();
			for (Iterator<?> iters = list.iterator(); iters.hasNext();) {
				Integer id = (Integer) iters.next();
				set.add(id);
			}
			return set;
		} finally {
			if (sqlSession != null)
				sqlSession.close();
		}
	}

	public List<?> selAllProgram() throws SQLException {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession(true);
			return sqlSession.selectList("menutree.selAllProgram");
		} finally {
			if (sqlSession != null)
				sqlSession.close();
		}
	}

	public Program loadProgramInfo(int programid) throws SQLException {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession(true);
			return (Program) sqlSession.selectOne("menutree.loadProgram", programid);
		} finally {
			if (sqlSession != null)
				sqlSession.close();
		}
	}

	public void saveProgramInfo(Program info, boolean isInsert) throws SQLException {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession(true);
			if (isInsert)
				sqlSession.insert("menutree.insertProgramList", info);
			else
				sqlSession.update("menutree.updateProgramList", info);
		} finally {
			if (sqlSession != null)
				sqlSession.close();
		}
	}

	public void delProgram(int programid) throws SQLException {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession(true);
			sqlSession.delete("menutree.delProgramList", programid);
		} finally {
			if (sqlSession != null)
				sqlSession.close();
		}
	}

	public int getMenuTemplateId(int userid, int entid) throws SQLException {
		int menuTemplateId = -1;
		StringBuilder sql = new StringBuilder();
		sql.append("select menutemplateid from menutemplate a");
		sql.append(" inner join userdeptorg b on a.orgid=b.orgid ");
		sql.append(" where b.userid=? ");
		sql.append(" union ");
		sql.append(" select menutemplateid from menutemplate a");
		sql.append(" where a.entid=? ");
		try {
			Connection	conn = DBUtil.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, userid);
			ps.setInt(2, entid);
			ResultSet rst = ps.executeQuery();
			if (rst.next()) {
				menuTemplateId = rst.getInt(1);
			}
			rst.close();
			ps.close();
		} finally {
			DBUtil.close();
		}
		return menuTemplateId;
	}
}
